package com.smart.member.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.smart.common.constants.MessageConstants;
import com.smart.common.dto.integral.IntegralDto;
import com.smart.common.exception.ServiceException;
import com.smart.common.redis.dao.RedisDao;
import com.smart.common.vo.ResponseResult;
import com.smart.member.common.reqeust.MemberRequestParams;
import com.smart.member.common.utils.MemberUtils;
import com.smart.member.entity.Member;
import com.smart.member.mapper.MemberMapper;
import com.smart.member.service.MemberService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

import static com.smart.common.vo.ResponseEnumCode.*;

/**
 * 保证核心业务的并发
 * 解决并发
 *
 * @author zhangwei
 */
@Service
public class MemberServiceImpl implements MemberService {
    public static final String KEY_REDIS_PHONE_PREFIX = "member:phone:";
    public static final String REGISTER_TYPE_PHONE = "phone";
    public static final String REGISTER_TYPE_EMAIL = "email";

    @Resource
    RocketMQTemplate rocketMQTemplate;

    @Resource
    RedisDao redisDao;

    @Resource
    MemberMapper memberMapper;

    @Override
    public ResponseResult<String> code(String phone) {
        /**
         * 生成随机的验证码
         */
        String code = RandomUtil.randomNumbers(6);
        redisDao.setEx(KEY_REDIS_PHONE_PREFIX + phone, code, 60, TimeUnit.SECONDS);
        return ResponseResult.success(code);
    }

//    @Override
//    public ResponseResult<String> code(String phone) {
//        /**
//         * 生成随机的验证码
//         */
//        String code = RandomUtil.randomNumbers(6);
//        MemberMsgDto memberMsgDto = new MemberMsgDto();
//        memberMsgDto.setCode(code);
//        memberMsgDto.setPhone(phone);
//        // 通过MQ发送异步消息
//        rocketMQTemplate.asyncSend(MessageConstants.MQ_MEMBER_TOPIC, memberMsgDto, new DefaultSendCallback());
//        //发送消息给短信服务
//        //数据校验
//        redisDao.setEx(KEY_REDIS_PHONE_PREFIX + phone, code, 60, TimeUnit.SECONDS);
//        return ResponseResult.success("发送验证码成功");
//    }


    /**
     * 送积分
     *
     * @param member
     * @return 0 表示失败   1 表示成功
     */
    @Override
    public int register(MemberRequestParams member) {
        int count = 0;
        Member member1 = null;
        if (REGISTER_TYPE_PHONE.equals(member.getRegisterType())) {
            if (memberMapper.selectByPhone(member.getPhone()) == null) {
                String code = (String) redisDao.get(KEY_REDIS_PHONE_PREFIX + member.getPhone());
                if (!StringUtils.isEmpty(code)) {
                    if (code.equals(member.getCode())) {
                        member1 = new Member();
                        member1.setUsername(MemberUtils.getStringRandom(10));
                        member1.setPhone(member.getPhone());
                        member1.setPassword(member.getPassword());
                        count = memberMapper.insert(member1);

                        // 删除redis的验证码
                        redisDao.delete(KEY_REDIS_PHONE_PREFIX + member.getPhone());
                    } else {
                        throw new ServiceException(MEMBER_VERIFICATION_ERROR);
                    }
                } else {
                    throw new ServiceException(MEMBER_VERIFICATION_EXPIRE);
                }
            } else {
                throw new ServiceException(REGISTER_PHONE_EXIST);
            }
        } else if (REGISTER_TYPE_EMAIL.equals(member.getRegisterType())) {
            member1 = new Member();
            //生成随机用户名
            member1.setUsername(MemberUtils.getStringRandom(18));
            member1.setEmail(member.getEmail());
            member1.setPassword(member.getPassword());
            count = memberMapper.insert(member1);
        }
        if (count > 0) {
            IntegralDto integralDto = new IntegralDto();
            integralDto.setMemberId(member.getMemberId());
            integralDto.setScore(200);
            integralDto.setType(2);
            //解耦 应用程序
            rocketMQTemplate.asyncSend(MessageConstants.MEMBER_INTEGRAL_TOPIC, integralDto, null);
        }
        return count;
    }

    /**
     * @param username
     * @return
     */
    @Override
    public Member getMemberInfo(String username) {
        return memberMapper.selectByUserName(username);
    }

    @Override
    public Member login(String username, String code) {
        return null;
    }

    @Override
    public Member findMember(String type, String username) {
        return null;
    }

}
